<template>
	<view class="template-set">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left-arrow'></text>
			</view>
		</tn-nav-bar>

		<view class="tn-margin-top" :style="{paddingTop: vuex_custom_bar_height + 'px'}">


			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding" @click="tn('/minePages/avatar')">
				<view class="justify-content-item">
					<view class="tn-text-bold tn-text-lg">
						用户头像
					</view>
					<view class="tn-color-gray tn-padding-top-xs">
						有趣的头像，百里挑一
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey">
					<view class="logo-pic tn-shadow">
						<view class="logo-image">
							<view class="tn-shadow-blur"
								:style="{backgroundImage:'url('+user.avatar+');width: 80rpx;height: 80rpx;background-size: cover;'}">
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding" @click="showModal1">
				<view class="justify-content-item">
					<view class="tn-text-bold tn-text-lg">
						用户昵称
					</view>
					<view class="tn-color-gray tn-padding-top-xs">
						{{user.nickName}}
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey">
					<view class="tn-icon-right tn-padding-top"></view>
				</view>
			</view>

			<view class="tn-flex tn-flex-row-between tn-strip-bottom tn-padding">
				<view class="justify-content-item">
					<view class="tn-text-bold tn-text-lg">
						绑定手机号(暂时不允许更改)
					</view>
				</view>
				<view class="tn-color-gray tn-padding-top-xs">
					{{user.phonenumber}}
				</view>

			</view>


			<picker @change="bindPickerChange" :value="sex" range-key="dictLabel" :range="listSex">
				<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding">
					<view class="justify-content-item">
						<view class="tn-text-bold tn-text-lg">
							性别
						</view>
						<view class="tn-color-gray tn-padding-top-xs">

							<view class="tn-color-gray">{{showDIct(sex,listSex)}}</view>
						</view>
					</view>
					<view class="justify-content-item tn-text-lg tn-color-grey">
						<view class="tn-icon-right tn-padding-top"></view>
					</view>
				</view>
			</picker>

			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding" @click="popup.show = true">
				<view class="justify-content-item">
					<view class="tn-text-bold tn-text-lg" v-if="user.roleState == 0">
						角色 (待审核)
					</view>
					<view class="tn-text-bold tn-text-lg" v-if="user.roleState == 1">
						角色 (已审核)
					</view>
					<view class="tn-color-gray tn-padding-top-xs">
						{{showDIct(role,listRole)}}
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey">
					<view class="tn-icon-right tn-padding-top"></view>
				</view>
			</view>


			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding" @click="showModal2">
				<view class="justify-content-item">
					<view class="tn-text-bold tn-text-lg">
						密码
					</view>
					<view class="tn-color-gray tn-padding-top-xs">
						不准偷看!
					</view>
				</view>
				<view class="justify-content-item tn-text-lg tn-color-grey">
					<view class="tn-icon-right tn-padding-top"></view>
				</view>
			</view>

			<tn-modal v-model="show1" :custom="true" :showCloseBtn="true">
				<view class="custom-modal-content">
					<view class="">
						<view class="tn-text-lg tn-text-bold tn-color-purplered tn-text-center tn-padding">修改昵称</view>
						<view class="tn-bg-gray--light"
							style="border-radius: 10rpx;padding: 20rpx 30rpx;margin: 50rpx 0 60rpx 0;">
							<tn-input placeholder="请输入昵称" name="input" v-model="nickName" inputAlign="left"></tn-input>

						</view>
					</view>
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button backgroundColor="#3668FC" padding="40rpx 0" width="60%" shadow fontBold
							@click="setNickName()">
							<text class="tn-color-white">保 存</text>
						</tn-button>
					</view>
				</view>
			</tn-modal>

			<tn-modal v-model="show2" :custom="true" :showCloseBtn="true">
				<view class="custom-modal-content">
					<view class="">
						<view class="tn-text-lg tn-text-bold tn-color-purplered tn-text-center tn-padding">新密码</view>
						<view class="tn-bg-gray--light"
							style="border-radius: 10rpx;padding: 20rpx 30rpx;margin: 50rpx 0 60rpx 0;">
							<tn-input placeholder="请输入新密码" name="input" v-model="newPwd" inputAlign="left"></tn-input>

						</view>
					</view>
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button backgroundColor="#3668FC" padding="40rpx 0" width="60%" shadow fontBold
							@click="updatePwd()">
							<text class="tn-color-white">保 存</text>
						</tn-button>
					</view>
				</view>
			</tn-modal>



			<!-- 接手前上传图片 -->
			<tn-popup v-model="popup.show" :marginTop="vuex_custom_bar_height" length="50%" :mode="popup.mode"
				:backgroundColor="popup.backgroundColor" :width="popup.width" :height="popup.height"
				:borderRadius="popup.borderRadius" :closeBtn="popup.closeBtn" :closeBtnIcon="popup.closeBtnIcon"
				:closeBtnPosition="popup.closeBtnPosition" :maskCloseable="popup.maskCloseable"
				@close="popup.closedPopup">
				<view class="popup-content" :class="{'popup-content--center': mode === 'center'}">

					<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
						<view class="tn-flex justify-content-item">
							<view class="tn-bg-black tn-color-white tn-text-center"
								style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
								<text class="tn-icon-my" style="font-size: 30rpx;"></text>
							</view>
							<view class="tn-text-lg tn-padding-right-xs tn-text-bold">选择角色</view>
						</view>
						<tn-input v-model="roleName" type="select" placeholder="请选择角色" :border="border"
							:selectOpen="actionSheetShow" @click="actionSheetShow = true" inputAlign="right"></tn-input>
					</view>
					<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
						<view class="tn-flex justify-content-item">
							<view class="tn-bg-black tn-color-white tn-text-center"
								style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
								<text class="tn-icon-image" style="font-size: 30rpx;"></text>
							</view>
							<view class="tn-text-lg tn-padding-right-xs tn-text-bold">凭证上传(务必上传!)</view>
						</view>
						<view class="justify-content-item tn-text-df tn-color-grey" @tap="clear">
							<text class="tn-padding-xs">清空上传</text>
							<text class="tn-icon-delete"></text>
						</view>
					</view>

					<view class="tn-margin-left tn-padding-top-xs">
						<view class="tn-margin-left tn-padding-top-xs">
							<tn-image-upload-drag ref="imageUpload" :action="imgUpload.action" :width="236"
								:height="236" :header="imgUpload.formData" :formData="imgUpload.formData"
								:fileList="imgUpload.fileList" :disabled="imgUpload.disabled"
								:autoUpload="imgUpload.autoUpload" :maxCount="imgUpload.maxCount"
								:showUploadList="imgUpload.showUploadList" :showProgress="imgUpload.showProgress"
								:deleteable="imgUpload.deleteable" :customBtn="imgUpload.customBtn"
								@sort-list="onSortList" @on-success="success" />
						</view>
					</view>


					<view class="tn-flex tn-flex-row-between tn-footerfixed">
						<view class="tn-flex-2 justify-content-item tn-margin-xs tn-text-center">
							<tn-button backgroundColor="#00FFC6" padding="40rpx 0" width="90%" shadow fontBold
								@click="upload()">
								<text class="tn-icon-add tn-padding-right-xs tn-color-black"></text>
								<text class="tn-color-black">角 色 修 改</text>
							</tn-button>
						</view>
					</view>
				</view>
			</tn-popup>

			<tn-select ref="roleSelect" v-model="actionSheetShow" mode="single" title="选择角色" :list="listRole"
				:searchShow="false" @confirm="actionSheetClick" valueName="dictValue" labelName="dictLabel">
			</tn-select>

		</view>


	</view>
</template>

<script>
	import request from '../common/request';
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	export default {
		name: 'TemplateSet',
		mixins: [template_page_mixin],
		data() {
			return {
				user: {},
				show1: false,
				show2: false,
				sex: '',
				listSex: [],
				role: '',
				listRole: [],
				nickName: "",
				newPwd: "",
				popup: {
					show: false,
					mode: 'bottom',
					backgroundColor: '',
					width: '',
					height: '',
					borderRadius: 0,
					closeBtn: true,
					closeBtnIcon: 'close',
					closeBtnPosition: 'top-right',
					maskCloseable: true,
				},
				actionSheetShow: false,
				roleName:"",
				imgUpload: {
					action: request.getUpload(), // action: '',
					formData: {
						apiType: 'this,ali',
						Authorization: "Bearer " + uni.getStorageSync("token"),
						image: null
					},
					fileList: [
				
					],
					showUploadList: true,
					customBtn: false,
					autoUpload: false,
					showProgress: false,
					deleteable: true,
					customStyle: false,
					maxCount: 9,
					disabled: false,
				},
				fileUrl: "",
			}
		},
		computed: {

		},
		mounted() {
			this.loadData();
		},
		methods: {
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			//加载登录用户信息 与 字典
			loadData() {
				this.user = uni.getStorageSync("userinfo");
				this.nickName = this.user.nickName;
				this.sex = this.user.sex;
				this.role = this.user.roleApp;

				request.get("/system/dict/data/lists", {
					dictType: 'xxc_role,sys_user_sex'
				}).then((res) => {
					this.listRole = res.xxc_role;
					this.listSex = res.sys_user_sex;
				});
			},
			// 点击actionSheet选择角色
			actionSheetClick(event) {
				this.role = event[0].value;
				this.roleName = event[0].label;
			},
			// 手动上传文件
			upload() {
				this.$refs.imageUpload.upload()
			},
			// 手动清空列表
			clear() {
				this.$refs.imageUpload.clear()
			},
			success(data, index, array, indexNow) {
				this.fileUrl += data.fileName + ',';
				if (index + 1 == array.length) {
					let img = this.fileUrl.substring(0, this.fileUrl.length - 1);
					this.user.roleApp = this.role;
					this.user.roleImg = img;
					this.user.roleState = 0;
					
					request.post("/system/user/updateAppUser", this.user).then((res) => {
						uni.setStorageSync("userinfo", this.user)
						uni.showModal({
							title: '提示：',
							content: res.msg,
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: '/minePages/set'
									});
								}
							}
						});
					});
				} else {
					this.$t.message.toast('请上传凭证');
				}
			},
			// 图片拖拽重新排序
			onSortList(list) {
				console.log(list);
			},
			// 弹出模态框
			showModal1(event) {
				this.openModal1()
			},
			// 打开模态框
			openModal1() {
				this.show1 = true
			},
			// 弹出模态框
			showModal2(event) {
				this.openModal2()
			},
			// 打开模态框
			openModal2() {
				this.show2 = true
			},
			setNickName(e) {
				this.updateAppUser()
				this.show1 = false;
			},
			bindPickerChange: function(e) {
				this.sex = this.listSex[e.detail.value].dictValue
				this.updateAppUser()
			},

			bindPickerChange1: function(e) {
				this.role = this.listRole[e.detail.value].dictValue
				this.updateAppUser()
			},
			showDIct(value, array) {
				for (let i = 0; i < array.length; i++) {
					if (array[i].dictValue == value) {
						return array[i].dictLabel
					}
				}
			},
			updateAppUser(name) {
				this.user.roleApp = this.role;
				this.user.nickName = this.nickName;
				this.user.sex = this.sex;
				request.post("/system/user/updateAppUser", this.user).then((res) => {
					uni.setStorageSync("userinfo", this.user)
					uni.showModal({
						title: '提示：',
						content: res.msg,
						success: function(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: '/minePages/set'
								});
							}
						}
					});
				});
			},
			updatePwd() {
				request.post("/WxOpenApi/appfgassword", {
					"username": this.user.userName,
					"passwordnew": this.newPwd
				}).then((res) => {
					uni.showModal({
						title: '提示：',
						content: res.msg,
						success: function(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: '/minePages/set'
								});
							}
						}
					});
				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 60%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

	}


	/* 间隔线 start*/
	.tn-strip-bottom-min {
		width: 100%;
		border-bottom: 1rpx solid #F8F9FB;
	}

	.tn-strip-bottom {
		width: 100%;
		border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}

	/* 间隔线 end*/


	/* 用户头像 start */
	.logo-image {
		width: 80rpx;
		height: 80rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 2rpx solid rgba(255, 255, 255, 0.05);
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}


	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 100rpx;
		height: calc(120rpx + env(safe-area-inset-bottom) / 2);
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);

	}

	/* 底部悬浮按钮 end*/
</style>